package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/global"
	"hft/tools"
	"regexp"
)

type Activity struct{}

func (*Activity) CreateActivity(claims tools.BaseClaims, data *admin.Activity) (err error) {

	var account *admin.Account

	var louPan *admin.LouPan

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	err = global.OC_DB.First(&louPan, data.LouPanId).Error

	if err != nil {
		return errors.New("找不到该楼盘信息")
	}

	res := global.OC_DB.Where("title = ? AND lou_pan_id = ?", data.Title, data.LouPanId).FirstOrCreate(&data)

	if res.Error != nil {
		return errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该活动已存在")
	}

	return nil
}

func (*Activity) EditActivity(claims tools.BaseClaims, data *admin.Activity) (err error) {
	var account *admin.Account

	var louPan *admin.LouPan

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("无该账号信息")
	}

	err = global.OC_DB.First(&louPan, data.LouPanId).Error

	if err != nil {
		return errors.New("找不到该楼盘信息")
	}

	res := global.OC_DB.Model(&admin.Activity{}).Where("id = ?", data.ID).Updates(map[string]interface{}{
		"sort":               data.Sort,
		"title":              data.Title,
		"cont":               data.Cont,
		"activity_sub":       data.ActivitySub,
		"cover":              data.Cover,
		"image":              data.Image,
		"init_num":           data.InitNum,
		"status":             data.Status,
		"button_name":        data.ButtonName,
		"back_color":         data.BackColor,
		"text_color":         data.TextColor,
		"border_color":       data.BorderColor,
		"button_color":       data.ButtonColor,
		"activity_sub_color": data.ActivitySubColor,
		"application":        data.Application,
		"success_button":     data.SuccessButton,
	})

	if res.Error != nil {
		return errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该活动已存在")
	}

	return nil
}

func (*Activity) GetActivityList(claims tools.BaseClaims, LouPanId int) (data []*admin.Activity, err error) {

	var account *admin.Account

	var louPan *admin.LouPan

	var activities []*admin.Activity

	if LouPanId == 0 {
		return nil, errors.New("无效的楼盘")
	}

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("找不到该账号信息")
	}

	err = global.OC_DB.First(&louPan, LouPanId).Error

	if err != nil {
		return nil, errors.New("找不到该楼盘信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), louPan.RegionLink)

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	if !match {
		return nil, errors.New("你无权获取楼盘活动")
	}

	err = global.OC_DB.Where("lou_pan_id = ?", LouPanId).Find(&activities).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	return activities, nil
}

var ActivityService = new(Activity)
